#include "mainWindow.h"
#include "ui_mainWindow.h"

#include "millerRabinTest.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->runButton, SIGNAL(clicked()),
            this, SLOT(runButtonClicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::runButtonClicked()
{
    QString inputString = ui->inputEdit->text();

    bool ok = false;

    quint64 inputNumber = inputString.toULongLong(&ok);

    // input check
    if(!ok)
    {
        ui->resultOutput->setText(QString("%1 is invalid. Please enter an valid number.").arg(inputString));
        ui->timeOutput->setText(QString("invalid"));

        return;
    }

    if(inputNumber < 2)
    {
        ui->resultOutput->setText(QString("%1 is invalid. Please enter a natural number greater than 1.").arg(inputString));
        ui->timeOutput->setText(QString("invalid"));
    }

    // test input
    QElapsedTimer timer;
    timer.start();

    bool result = MillerRabinTest::isPrimNumber(inputNumber);

    ui->timeOutput->setText(QString("%1 ns").arg(QString::number(timer.nsecsElapsed())));

    if(result)
        ui->resultOutput->setText(QString("%1 is a prime number").arg(inputString));
    else
        ui->resultOutput->setText(QString("%1 is not a prime number").arg(inputString));
}
